package com.xxkk.spring_boot_web.sdk.google;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken.Payload;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;

/**
 * mkdir /tmp/foo && cd /tmp/foo
 * wget https://dl.google.com/dl/android/maven2/com/google/android/gms/play-services-basement/8.3.0/play-services-basement-8.3.0.aar
 * unzip play-services-basement-8.3.0.aar
 * mvn install:install-file \
 * -Dfile=classes.jar \
 * -DgroupId=com.google.android.google-play-services \
 * -DartifactId=google-play-services \
 * -Dversion=1 \
 * -Dpackaging=jar
 * cd -
 *
 * <dependency>
 * <groupId>com.google.api-client</groupId>
 * <artifactId>google-api-client</artifactId>
 * <version>1.33.0</version>
 * </dependency>
 */
public class GoogleSdk {
    // 替换为你的客户端ID
    private static final String CLIENT_ID = "275385888928-00d2uaaod9gepu763vep5j6le54111vl.apps.googleusercontent.com";

    public static void main(String[] args) {
        // 从客户端接收的ID令牌（示例值）
        String idTokenString = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImUxNGMzN2Q2ZTVjNzU2ZThiNzJmZGI1MDA0YzBjYzM1NjMzNzkyNGUiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIyNzUzODU4ODg5MjgtdjlyYzAyb2d1ZzJzbmZzbTJlbmtlOWFvc2FzOTJsMDUuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiIyNzUzODU4ODg5MjgtMDBkMnVhYW9kOWdlcHU3NjN2ZXA1ajZsZTU0MTExdmwuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDU4NDAyOTM5NTgzNDgyNzk4NDMiLCJlbWFpbCI6ImNoZW56aHVvamF5QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJuYW1lIjoiemh1byBjaGVuIiwicGljdHVyZSI6Imh0dHBzOi8vbGgzLmdvb2dsZXVzZXJjb250ZW50LmNvbS9hL0FDZzhvY0lIRGJ0SmtKb2htdDFYenNGU2prRFJMR3Y2QVBhM3AxdnQyaUQwcFZxLWhqODJUZz1zOTYtYyIsImdpdmVuX25hbWUiOiJ6aHVvIiwiZmFtaWx5X25hbWUiOiJjaGVuIiwiaWF0IjoxNzQ3MDQzNzk3LCJleHAiOjE3NDcwNDczOTd9.AQiPni3ws2Sy4eYZvqBzXWlMCxWflSykQg0arHzEovg45Us6wQGV1TGNma6mZwFSYmXJHYF5Bi3cuJKKZPMKUERhP2k0neLW9u15XR0pZ8sj8CgAPlkYhoEZ7qrkXKaurimKuKNmxXG6mxEHNENEf5mRzWsOlHG00WMulvSu1xGoRIj4Zp58Lvvse1IfEA5xf_0E2wrV5gkrWcXPcFW7epsXqivf_kTm6HklMZQDiKNnmfxmYpeZXFFTKTFQkdMwFEurPr6JsHafM4tEJ46oBgB-ricb5oMB-1ZFQbyRWNw1ozQWVWzEqW_pODRtOK7wtN836zwb8WQ4HrDyLp_zmw";

        try {
            verifyGoogleToken(idTokenString);
        } catch (GeneralSecurityException | IOException e) {
            System.err.println("验证过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void verifyGoogleToken(String idTokenString)
            throws GeneralSecurityException, IOException {
        // 初始化HTTP传输和JSON工厂
        NetHttpTransport transport = new NetHttpTransport();
        GsonFactory jsonFactory = GsonFactory.getDefaultInstance();

        // 创建验证器
        GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
                .setAudience(Collections.singletonList(CLIENT_ID))
                .build();

        verifier.getPublicKeysManager().refresh();
        // 验证令牌
        GoogleIdToken idToken = verifier.verify(idTokenString);

        if (idToken == null) {
            System.out.println("无效的ID令牌");
            return;
        }

        // 提取用户信息
        Payload payload = idToken.getPayload();
        printUserInfo(payload);
    }

    private static void printUserInfo(Payload payload) {
        // 获取用户唯一标识
        String userId = payload.getSubject();
        System.out.println("用户ID: " + userId);

        // 获取用户基本信息
        String email = payload.getEmail();
        boolean emailVerified = payload.getEmailVerified();
        String name = (String) payload.get("name");
        String pictureUrl = (String) payload.get("picture");
        String locale = (String) payload.get("locale");
        String familyName = (String) payload.get("family_name");
        String givenName = (String) payload.get("given_name");

        // 打印用户信息
        System.out.println("电子邮件: " + email);
        System.out.println("邮件已验证: " + emailVerified);
        System.out.println("姓名: " + name);
        System.out.println("姓氏: " + familyName);
        System.out.println("名字: " + givenName);
        System.out.println("头像URL: " + pictureUrl);
        System.out.println("语言区域: " + locale);
    }
}
